<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-29
 * Time: 13:48
 */

namespace backend\controllers;


use app\models\CtiCdr;
use app\models\CtiCdrWav;
use app\models\search\CtiCdrSearch;
use yii\filters\AccessControl;

class CtiManagerCdrController  extends CtiManagerController
{

    public function behaviors()
    {
        return [
            'filter' => [
                'class' => AccessControl::className(),
                'only' => ['insert-cdr'],
            ],
        ];
    }

    /**
     * 插入数据到cdr数据库中，就是话单数据
     */
    public function actionInsertCdr()
    {
        // 将接收到的数据xml格式的转化为json然后啊转化为对象的格式，然后记录到数据库中
        $cdr = simplexml_load_string($_POST['cdr']);
        //将相应的数据转化为相应的符合的数据,，不然拿到的是xml的对象数据
        $cdr = json_decode(json_encode($cdr));
        $variables = $cdr->variables;
        $model = new CtiCdr();
        if ($variables->direction == "inbound"){//为呼出
            $model->user_name = isset($variables->user_name)?$variables->user_name:"";
            $model->sip_from_user = isset($variables->sip_from_user)?$variables->sip_from_user:"";
            $model->sip_to_user = isset($variables->sip_to_user)?substr($variables->sip_to_user,1,strlen($variables->sip_to_user)):"";
            $model->user_context = isset($variables->user_context)?$variables->user_context:"";
            $model->continue_on_fail = isset($variables->continue_on_fail)?$variables->continue_on_fail:"";
            $model->bridge_uuid = isset($variables->bridge_uuid)?$variables->bridge_uuid:"";
            //查询电话号码归属地 1896887
            $telephone_assignment = $this->actionSearchTelephoneAssignment(isset($model->sip_to_user)?substr($model->sip_to_user,0,6):"");
        }else{//为呼入
            $model->user_name = $variables->dialed_user;
            $model->sip_from_user = $variables->sip_from_user;
            $model->sip_to_user = $variables->dialed_user;
            $model->user_context = $cdr->callflow->caller_profile->context;
            $model->continue_on_fail = $variables->hangup_cause;
            $model->bridge_uuid = isset($variables->bridge_uuid)?$variables->bridge_uuid:"";
            $telephone_assignment = $this->actionSearchTelephoneAssignment(substr($model->sip_from_user,0,7));
        }
        $model->direction = $variables->direction;
        $model->telephone_assignment = $telephone_assignment['province']['ProName']."/".$telephone_assignment['city']['CityName'];
        $model->start_epoch = $variables->start_epoch;
        $model->answer_epoch = $variables->answer_epoch;
        $model->end_epoch = $variables->end_epoch;
        $model->duration = $variables->duration;
        $model->billsec = $variables->billsec;
        $model->call_uuid = $variables->call_uuid;
        $model->create_at = ''.time();
        $model->uuid = isset($variables->cc_member_session_uuid)?$variables->cc_member_session_uuid:"";//重要，必须要设置uuid
        //如果通话的不是中间中转的就不保存
        if (strpos($model->sip_to_user,"2B")===false && $model->save()){
            $res = CtiCdrWav::findOne(['uuid'=>$model->uuid]);
            if ($res){
                $final = $res->updateAttributes(['zuoxi'=>$model->user_name]);
                return $final;
            }else{
                return $res;
            }
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 话单列表
     */
    public function actionList($type)
    {
        $params['controller'] = $this->https_url."uploads/download";
        if ($type=="myCdr"){//我的话单
            return $this->getSearchList(new CtiCdrSearch(),$_POST,$_POST['page'],$_POST['limit'],$params);
        }else{//总话单
            return $this->getSearchList(new CtiCdrSearch(),$_POST,$_POST['page'],$_POST['limit'],$params);
        }
    }

}